﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for HocVienDAL
/// </summary>
public class HocVienDAL
{
    public static object IsNull(object checkValue, object replaceValue)
    {
        if (checkValue.Equals(System.DBNull.Value) || checkValue == null)
            return replaceValue;
        return checkValue;
    }

    public static List<HocVienINFO> listAll()
    {
        string sql = "select * from HOCVIEN";
        SqlDataReader dr = SQLHelper.ExecuteReader(Conn.ConnectionString, CommandType.Text, sql, null);
        return SQLHelper.SetListObjects<HocVienINFO>(dr);
    }

    public static HocVienINFO GetInfo(int mahocvien)
    {
        string sql = "select * from HOCVIEN where MaHV=" + mahocvien + "";
        SqlDataReader dr = SQLHelper.ExecuteReader(Conn.ConnectionString, CommandType.Text, sql, null);
        return SQLHelper.SetObject<HocVienINFO>(dr);
    }

    public static int Insert(HocVienINFO obj)
    {
        obj.MaHV = GetMa();
        obj.MaHV = obj.MaHV + 1;
        string sql = "INSERT INTO HOCVIEN (MaHV,TenHV,GioiTinh,NgaySinh,DiaChi,SDT,Email) VALUES (" + obj.MaHV + ",N'" + obj.TenHV + "'," + obj.GioiTinh + " ,'" + obj.NgaySinh + "',N'" + obj.DiaChi + "','" + obj.SDT + "','" + obj.Email + "')";
        return SQLHelper.ExecuteNonQuery(Conn.ConnectionString, CommandType.Text, sql, null);
    }

    public static int Update(HocVienINFO obj)
    {
        string sql = "Update HOCVIEN set TenHV=N'" + obj.TenHV + "', GioiTinh=" + obj.GioiTinh + ", NgaySinh='" + obj.NgaySinh + "',DiaChi=N'" + obj.DiaChi + "',SDT='" + obj.SDT + "',Email='" + obj.Email + "' Where MaHV=" + obj.MaHV + "";
        return SQLHelper.ExecuteNonQuery(Conn.ConnectionString, CommandType.Text, sql, null);
    }

    public static int DoiMatKhau(string matkhau, int MaHV)
    {
        string sql = "Update HOCVIEN set MatKhau='" + matkhau + "' Where MaHV=" + MaHV + "";
        return SQLHelper.ExecuteNonQuery(Conn.ConnectionString, CommandType.Text, sql, null);
    }

    public static int Delete(int mahocvien)
    {
        string sql = "Delete from HOCVIEN where MaHV =" + mahocvien + "";
        return SQLHelper.ExecuteNonQuery(Conn.ConnectionString, CommandType.Text, sql, null);
    }

    public static int GetMa()
    {
        string sql = "SELECT ISNULL(MAX(MaHV),0) FROM HOCVIEN";
        SqlDataReader dr = SQLHelper.ExecuteReader(Conn.ConnectionString, CommandType.Text, sql, null);
        int ma = 0;
        if (dr.Read())
        {
            ma = int.Parse(dr[0].ToString());
        }
        dr.Close();
        return ma;
    }

    public static HocVienINFO DangNhap(string email)
    {
        string sql = "select * from HOCVIEN where email='" + email + "'";
        SqlDataReader dr = SQLHelper.ExecuteReader(Conn.ConnectionString, CommandType.Text, sql, null);
        return SQLHelper.SetObject<HocVienINFO>(dr);
    }
}